package com.ffyc.resale.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ffyc.resale.dao.GoodsDao;
import com.ffyc.resale.model.Goods;
import com.ffyc.resale.model.Result;
import com.ffyc.resale.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    @PostMapping("/list")
    public Result list(@RequestHeader String adminToken, @RequestBody Goods goods){
        IPage<Goods> list = goodsService.list(goods);
        return new Result(200,"成功",list);
    }
    @PostMapping("/search")
    public Result search(@RequestHeader String adminToken, @RequestBody Goods goods){
       IPage<Goods> list = goodsService.search(goods);
        return new Result(200,"成功",list);
    }
    //修改状态通过
    @PostMapping("/approve")
    public Result approve(@RequestHeader String adminToken, @RequestBody Goods id){
        goodsService.approve(id.getId());
        return new Result(200,"成功",null);
    }
    //修改状态拒绝
    @PostMapping("/reject")
    public Result reject(@RequestHeader String adminToken, @RequestBody Goods id){
        goodsService.reject(id.getId());
        return new Result(200,"成功",null);
    }
    /**查询某用户所卖的商品*/
    @PostMapping("/myGoods")
    public Result myGoods(@RequestHeader String userToken, @RequestBody Goods goods){
        Page<Goods> goodsPage = goodsService.myGoods(userToken,goods);
        return new Result(200,"查询成功",goodsPage);
    }
    /** 下架商品 */
    @PostMapping("/delist/{id}")
    public Result delist(@RequestHeader String userToken,@PathVariable Integer id){
        goodsService.delist(id);
        return new Result(200,"下架成功",null);
    }
    /** 修改或重新上架商品信息 */
    @PostMapping("/update")
    public Result update(@RequestHeader String userToken,@RequestBody Goods goods) {
        goodsService.update(goods);
        return new Result(200,"下架成功",null);
    }
    //用户添加商品
    @PostMapping("/addGoods")
    public Result addGoods(@RequestHeader String userToken,@RequestBody Goods goods){
        goodsService.addGoods(userToken,goods);
        return new Result(200,"成功",true);
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@RequestHeader String userToken,@PathVariable Integer id){
        goodsService.delete(id);
        return new Result(200,"删除成功",null);
    }

    // 首页最新商品展示
    @RequestMapping(value = "selectNewGoods",method = RequestMethod.GET)
    public Result selectNewGoods(){
        List<Goods> goodsList = goodsService.selectNewGoods();
        return new Result(200,"查询成功",goodsList);
    }
}
